define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order_travel/index' + location.search,
                    add_url: 'order/order_travel/add',
                    // edit_url: 'order/order_travel/edit',
                    // del_url: 'order/order_travel/del',
                    multi_url: 'order/order_travel/multi',
                    import_url: 'order/order_travel/import',
                    table: 'travel_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('下单用户'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'num', title: __('Num')},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        // {field: 'name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        // {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.status},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'express_company', title: __('Express_company'), operate: 'LIKE'},
                        // {field: 'express_no', title: __('Express_no'), operate: 'LIKE'},
                        // {field: 'ship_time', title: __('Ship_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'complete_time', title: __('Complete_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        
                        {field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'refund',
                                    text: __('退款'),
                                    title: __('退款'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-reply',
                                    url: 'order/order_travel/refund',
                                    visible: function(row) {
                                        return row.status === '3';
                                    },
                                    extend: 'data-area=["500px","400px"]'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        ship: function () {
            Controller.api.bindevent();
            
            // 监听物流公司选择
            $('select[name="express_company"]').change(function(){
                var code = $(this).find("option:selected").data("code");
                $("#express_code").val(code);
            });
        },
        refund: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
